#include "OLED.h"

/****************************************************/
#define	nop1() _nop_()
#define	uchar	unsigned char
#define	uint unsigned	int
void    writecommand(uchar cmd);	// ???
void    writedata(uchar dataa);//???
void    writedata1(uchar dataa);//???
void	disp_clear(void);
void	disp_full(void);
void	disp_col(void);
void	disp_row(void);
void	dispgraphiCS(void);//dispgraphiCS
void	dispblxstp(void);//dispgraphiCS
void	dispstr(uchar	d);//????,0????,1????
/*********************************************************************************************/
uchar str2[6]={0x3e,0x51,0x49,0x45,0x3e,0x00};//disp"0"
uchar str3[6]={0x7f,0x40,0x40,0x40,0x40,0x00};//disp"L"
/*********************************************************************************************/
uchar code_graph[]={

/*--  ??x??=128x64  --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0xC2,0x04,0xCC,0x20,0x00,0x9C,0x44,
0x34,0x04,0xC4,0x04,0x14,0x34,0xEC,0x04,0x00,0x00,0x00,0x40,0x40,0x40,0xFE,0x40,
0x40,0x40,0x00,0xFC,0x00,0x00,0xFC,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0x00,0x10,
0x10,0x90,0x90,0x90,0x90,0x92,0xFE,0x90,0x90,0x90,0x90,0xD0,0x08,0x08,0x00,0x00,
0x00,0x00,0x00,0x00,0xFE,0x24,0x24,0x24,0x24,0xFC,0x24,0x24,0x24,0x24,0xFE,0x00,
0x00,0x00,0x00,0x00,0x40,0x42,0x4C,0xC0,0x00,0x04,0x04,0x04,0xFC,0x44,0x44,0x44,
0x44,0x44,0xE4,0x04,0x00,0x00,0x00,0x10,0x08,0xC6,0x62,0x20,0x78,0x48,0x3E,0x40,
0xF8,0x00,0xC0,0x3E,0x22,0xA0,0x60,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x90,0xFC,0x03,0x00,0x80,0x42,0x22,
0x1A,0x0E,0xFF,0x06,0x0A,0x32,0x62,0xC3,0x02,0x00,0x00,0x40,0x60,0x20,0x3F,0x20,
0x10,0xD0,0x38,0x07,0x00,0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,
0x00,0xFF,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x40,0xC0,0xFF,0x00,0x00,0x00,0x00,
0x00,0x00,0xC0,0x70,0x03,0x02,0xFA,0x02,0x06,0x0B,0x12,0x02,0x02,0xC2,0x83,0x10,
0x60,0x40,0x00,0x00,0x80,0x40,0x40,0x3F,0x40,0xA0,0x90,0x0C,0x03,0x00,0x00,0x20,
0x20,0x1E,0x03,0x00,0x00,0x00,0x00,0x06,0x01,0xFF,0x00,0x00,0xE1,0x1D,0x05,0x7D,
0x45,0x33,0x80,0x4F,0x30,0xCF,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x01,
0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x01,0x00,0x00,0x00,0x02,
0x02,0x01,0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x80,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x80,0x00,0x00,0x00,0x80,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x80,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0x44,0x44,0x44,0x44,0x44,0xFF,
0x44,0x44,0x44,0x44,0xFC,0x04,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x41,0x41,0x41,
0x41,0x41,0xF9,0x59,0x45,0x45,0x43,0x41,0x41,0x60,0x40,0x00,0x00,0x02,0x02,0x82,
0x42,0x22,0xFA,0x2E,0x23,0x22,0x22,0x22,0x22,0xF2,0x12,0x02,0x03,0x02,0x00,0x00,
0x00,0xFF,0x01,0x39,0x47,0x81,0x00,0xFF,0x49,0x49,0xC9,0x49,0x49,0xC9,0xFF,0x00,
0x00,0x00,0x00,0x00,0x80,0x40,0x20,0x18,0x06,0x03,0x80,0x60,0x40,0x03,0x0C,0x10,
0x60,0xC0,0x80,0x00,0x00,0x00,0x00,0x08,0x09,0xC9,0x49,0x49,0x49,0x49,0x49,0xC9,
0x09,0x09,0x01,0x01,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x04,0x04,0x04,0x04,0x04,0x7F,
0x44,0x44,0x44,0x44,0x47,0x40,0x78,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x40,0xC0,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x01,0x00,
0x00,0x80,0x7F,0x09,0x09,0x09,0x09,0x49,0x49,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xFF,0x04,0x04,0x0C,0x07,0x00,0x7F,0x40,0x20,0x13,0x0C,0x1C,0x32,0x61,0x61,
0x00,0x00,0x00,0x00,0x00,0x20,0x60,0x38,0x24,0x22,0x21,0x20,0x20,0x24,0x28,0x30,
0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x04,0x04,0x04,0x04,0x04,0x0F,
0x00,0x00,0x40,0xC0,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00


};


void LCD_GPIOInit(void){
	
	__HAL_RCC_GPIOB_CLK_ENABLE();
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);

  /*Configure GPIO pin : PB0 */  //  A0
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

	__HAL_RCC_GPIOA_CLK_ENABLE();
 
  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3|GPIO_PIN_4, GPIO_PIN_RESET);

  /*Configure GPIO pin : PA3 */  //  RES  cs
  GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


}
void delay(unsigned int delay) {
     unsigned int i, j;
     for(i=0; i<delay; i++)
	       for( j=0; j<100; j++);
 }
extern SPI_HandleTypeDef hspi1;
void writecommand(uint8_t cmd){
	
	 int i;
	 CS(0);
	 A0(0);
	 HAL_SPI_Transmit(&hspi1,&cmd,1,1000);
	// CS(1);
 }
void writedata(uint8_t dat) {
    int i;
    CS(0);
    A0(1);
    HAL_SPI_Transmit(&hspi1,&dat,1,1000);
  //  CS(1);
}

void LCD_Power_SaveON(void){
   writecommand(0xAE);

   writecommand(0xA5);
	 HAL_Delay(250);

}
void LCD_Power_SaveOFF(void){
   writecommand(0xA4);
   HAL_Delay(100);
   writecommand(0xAF);
	 HAL_Delay(200);

}
void LCD_Init(void){
	LCD_GPIOInit();
	RES(1);
	delay(300);
  RES(0);
	delay(300);
	RES(1);
	delay(1000);
	writecommand(0xE2);

  delay(100);

	writecommand(0xA2);//0xa2??????1/9

	writecommand(0xA0);//0xA0??????00H??

	writecommand(0xC8);//0xc8??com????,?COM(n-1)? COM0

	writecommand(0x23);//vop??

	writecommand(0x81);//vop???

	writecommand(0x32);//vop??

	writecommand(0x2F);//0x2f??????????????????,??????

	writecommand(0xB0);//0xb0??????

	writecommand(0xAF);//0xaf????LCD??

	writecommand(0xA6);	//0xa6???????
}





///************************************************************************************/
//void writecommand(int cmd) //???
//     {
//       int i;
//       CS=0;
//       A0=0;
//       for(i=0;i<8;i++)
//     {
//       if(cmd&0x80)
//       SDA=1;
//       else
//       SDA=0;
//       SCL=0;
//       SCL=1;
//       cmd=cmd<<1;
//      }
//       CS=1;
//      }
///****************************************************/
//void writedata(int dat) //???
//     {
//       int i;
//       CS=0;
//       A0=1;
//       for(i=0;i<8;i++)
//     {
//       if(dat&0x80)
//       SDA=1;
//      else
//       SDA=0;
//       SCL=0;
//       SCL=1;
//      dat=dat<<1;
//      }
//       CS=1;
//      }
///****************************************************/
//void writedata1(int dat) //???  CS=1
//     {
//       int i;
//       CS=1;
//       A0=1;
//       for(i=0;i<8;i++)
//     {
//       if(dat&0x80)
//       SDA=1;
//      else
//       SDA=0;
//       SCL=0;
//       SCL=1;
//      dat=dat<<1;
//      }
//       CS=1;
//      }
///****************************************************/
//void delay(unsigned int delay) //????
//      {
//      unsigned int i, j;

//       for
//      (
//      i=0; i<delay; i++
//      )
//       for
//      (
//       j=0; j<100; j++
//      );
//      }
///****************************************************/
//void lcd_init(void)//???
//   {

//        RES=1;
//        delay(30);
//        RES=0;
//    	delay(30);
//    	RES=1;
//    	delay(100);

//        writecommand(0xE2);

//        delay(10);

//        writecommand(0xA2);//0xa2??????1/9

//        writecommand(0xA0);//0xA0??????00H??

//        writecommand(0xC8);//0xc8??com????,?COM(n-1)? COM0

//        writecommand(0x23);//vop??

//        writecommand(0x81);//vop???

//        writecommand(0x32);//vop??

//        writecommand(0x2F);//0x2f??????????????????,??????

//        writecommand(0xB0);//0xb0??????

//        writecommand(0xAF);//0xaf????LCD??

//        writecommand(0xA6);	//0xa6???????
//	}
/****************************************************/
 void	disp_clear(void)   //??
	{
          uchar	i,j;
	  for(i=0;i<9;i++)
	{
	  writecommand(0x00);
	  writecommand(0xb0+i);
          writecommand(0x00);
          writecommand(0x10);
          for(j=0;j<132;j++)
	  writedata(0x00);
	}
	}
/****************************************************/
 void	disp_full(void)    //??
	{
          uchar	i,j;
	  for(i=0;i<9;i++)
	{
	  writecommand(0x00);
	  writecommand(0xb0+i);
          writecommand(0x00);
          writecommand(0x10);
          for(j=0;j<132;j++)
	  writedata(0xFF);
	}
	}
 /****************************************************/
 void	disp_col(void)    //??
	{
          uchar	i,j;
	  for(i=0;i<9;i++)
	{
	  writecommand(0x00);
	  writecommand(0xb0+i);
          writecommand(0x00);
          writecommand(0x10);
          for(j=0;j<132;j++)
	  writedata(0xAA);
	}
	}
 /****************************************************/
 void	disp_row(void)     //??
	{
	 uchar	i,j;
	 for (i=0;i<9;i++)
        {
         writecommand(0xb0+i);
	 writecommand(0x00);
	 writecommand(0x10);
	 for(j=0;j<128;j++)
	{
	 writedata(0xFF);
	 writedata(0x00);
	}
	}
	}
 /****************************************************/
 void dispstr(uint8_t d)//disp"0",disp"L"
	{
	 uchar i,j,k;
	 for(i=0;i<9;i++)
	{
	 writecommand(0xb0+i);
	 writecommand(0x00);
	 writecommand(0x10);
	 for(j=0;j<22;j++)
	 if(d==0)
	{
	 for(k=0;k<6;k++)
	 writedata(str2[k]);
	}
	 else
	 for(k=0;k<6;k++)
	 writedata(str3[k]);
        }
	}
// /****************************************************/
// void	dispgraphiCS(void)   //???
//	{
//         uint	i,j;
//	 for(i=0;i<9;i++)
//	{
//         writecommand(0xb0+i);
//         writecommand(0x10);
//         writecommand(0x00);
//         for(j=0;j<128;j++)
//	 writedata(graph[i*128+j]);
//	 }
//         }

//  void	main()
//     {
//	  while(1)
//     {

//        lcd_init();       //???

//        disp_clear();     //??
//	delay(50);

//	disp_full();      //??
//        delay(150);


//        
//        disp_row();      //??
//        delay(200);
//        
//        dispstr(0);      //0??
//        delay(200);
//        
//        
//        dispgraphiCS();   //???
//	delay(200000);



//       // disp_col();      //??
//        delay(200);



//    //  dispstr(1);      //L??
//    //  delay(200);

//      }
//      }





